﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//zdm 2019.6.12
//寻路组件，返回下一个要走到的方格
public class RoadSelector : MonoBehaviour
{
    public MapManager mMapManager;
    public BaseSelector selector;
    private Selector selectorType;
    private int backTime;
    private Vector2Int result;
    private Selector defaultSelector;

    private Vector2Int lastPos;
    private Selector lastS;
    
    public void ChangeSelector(Selector c)
    {
        selectorType = c;
        switch(c)
        {
            case Selector.Random:
                selector = new RandomSelect();
                break;
            case Selector.Back:
                selector = new GoBack(backTime);
                break;
            case Selector.Goal:
                selector = new GoToGoal();
                break;
            case Selector.Goto:
                selector = new GoToSomewhere();
                break;
            case Selector.Start:
                selector = new GoToStart();
                break;
            case Selector.Left:
                selector = new GoLeft();
                break;
            case Selector.Right:
                selector = new GoRight();
                break;
            case Selector.Player:
                selector = new GoPlayer();
                break;
            case Selector.Steal:
                selector = new StealHome();
                break;
            case Selector.StealBack:
                selector = new StealStart();
                break;
            case Selector.Base:
            default:
                break;
        }
    }

    //前两个维度为xy坐标，第三个维度为tag
    public RoadSelectResult GetNextPos(Vector2Int pos, Selector defaultS = Selector.Random)
    {
        if(selectorType == Selector.Base)
        {
            defaultSelector = defaultS;
            ChangeSelector(defaultS);
        }
        //Debug.Log(selectorType.ToString());
        Vector2Int nextPos;
        if (pos == lastPos && selectorType == lastS)
            nextPos = result;
        else nextPos = selector.GetNextPos(pos);
        if(nextPos != pos)
        {
            lastPos = pos;
            lastS = selectorType;
        }
        RoadSelectTag rst = RoadSelectTag.Normal;
        if (pos == MapManager.Instance.goalPos)
            rst = RoadSelectTag.Goal;
        if (MapManager.Instance.IsTower(nextPos))
            rst = RoadSelectTag.Tower;
        //Debug.Log(selectorType.ToString());
        //Debug.Log("nextPos:" + MapManager.Instance.sDist[nextPos.x, nextPos.y].ToString() + "  " + MapManager.Instance.tDist[nextPos.x, nextPos.y].ToString());
        //int dis = MapManager.Instance.sDist[pos.x, pos.y] + MapManager.Instance.tDist[pos.x, pos.y];
        //寻路算法切换
        if (selectorType == Selector.Goto)
        {
            if (selector.destPos == nextPos)
            {
                ChangeSelector(defaultS);
            }
        }
        else if (selectorType != Selector.Goal && selectorType != Selector.Start && MapManager.Instance.tDist[nextPos.x, nextPos.y] < selector.GOALDIST)
        {
            ChangeSelector(Selector.Goal);
        }
        else if (selectorType != Selector.Back && selector.CheckBack() > BaseSelector.MAXBACK)
        {
            backTime++;
            ChangeSelector(Selector.Back);
        }
        else if (selectorType != defaultS && selector.CheckBack() < 0)
        {
            ChangeSelector(defaultS);
        }
        selector.Update(pos);
        result = nextPos;
        return new RoadSelectResult(nextPos.x, nextPos.y, rst);
    }

    public void SetDestinationPos(Vector2Int pos)
    {
        selectorType = Selector.Goto;
        ChangeSelector(selectorType);
        selector.SetDest(pos);
    }

    public void GoStart()
    {
        if (defaultSelector != Selector.Steal)
            selectorType = Selector.Start;
        else selectorType = Selector.StealBack;
        ChangeSelector(selectorType);
    }
    // Start is called before the first frame update
    void Start()
    {
        lastPos = ConstOfMap.ERRORPOS;
        lastS = Selector.Base;
    } 

    // Update is called once per frame
    void Update()
    {
        
    }
}
